package com.company.project.web;
import com.alibaba.fastjson.JSON;
import com.company.project.core.Result;
import com.company.project.core.ResultGenerator;
import com.company.project.model.Authors;
import com.company.project.model.Thesis;
import com.company.project.model.ThesisDelivery;
import com.company.project.service.ThesisDeliveryService;
import com.company.project.util.FileUploadUtil;
import com.company.project.util.ThesisDeliveryAndAuthors;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
* Created by CodeGenerator on 2018/10/08.
*/
@RestController
@RequestMapping("/thesis/delivery")
public class ThesisDeliveryController {
    @Resource
    private ThesisDeliveryService thesisDeliveryService;

    @Resource
    private FileUploadUtil fileUploadUtil;
    //论文投递
    @PostMapping("/add")
    @ResponseBody
    public Result add(String thesisDelivery,String authors,String thesis, @RequestParam(required = false) MultipartFile file) {
        ThesisDeliveryAndAuthors thesisDeliveryAndAuthors = new ThesisDeliveryAndAuthors();
        thesisDeliveryAndAuthors.setThesisDelivery(JSON.parseObject(thesisDelivery,ThesisDelivery.class));
        thesisDeliveryAndAuthors.setThesis(JSON.parseObject(thesis,Thesis.class));
        thesisDeliveryAndAuthors.setAuthors(JSON.parseArray(authors,Authors.class));
        String result="无上传图片操作";
        if(file!=null){
            result=fileUploadUtil.writeFile(file,"other");
            if(result==null){
                return ResultGenerator.getInternalServerErrot("图片上传错误");
            }
            thesisDeliveryAndAuthors.getThesis().setUri(result);
        }

        boolean tds =thesisDeliveryService.insertThesisDeliveryAndAuthor(thesisDeliveryAndAuthors);
        if(!tds){
            return ResultGenerator.getInternalServerErrot("数据保存错误");
        }
        return ResultGenerator.genSuccessResult(result);
    }

    @PostMapping("/delete")
    public Result delete(@RequestParam Integer id) {
        thesisDeliveryService.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(ThesisDelivery thesisDelivery) {
        thesisDeliveryService.update(thesisDelivery);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        ThesisDelivery thesisDelivery = thesisDeliveryService.findById(id);
        return ResultGenerator.genSuccessResult(thesisDelivery);
    }

    @PostMapping("/list")
    public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<ThesisDelivery> list = thesisDeliveryService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }
    @PostMapping("/selectNew")
    @ResponseBody
    public Integer selectNew(){
        return thesisDeliveryService.selectNew();
    }

    @GetMapping("/TDParticular/{id}")
    @ResponseBody
    public  List<Map<Object,Object>> selectTD(@PathVariable("id") Integer id){


        List<Map<Object,Object>> objectObjectMap = thesisDeliveryService.selectTD(id);
        SimpleDateFormat format =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (Map<Object,Object> map : objectObjectMap){
            map.put("delivery_time",format.format(map.get("delivery_time")));
        }
        return objectObjectMap;
    }
}
